package cn.dapeng.tree.multi;

import java.util.ArrayList;
import java.util.List;

public class MultiNode {

    public int value;
    public List<MultiNode> children;

    public MultiNode(int value) {
        this.value = value;
    }

    public static MultiNode builder(int maxLevel, int childrenNum, int maxValue) {
        return builder(0, maxLevel, childrenNum, maxValue);
    }

    public static MultiNode builder(int level, int maxLevel, int childrenNum, int maxValue) {
        if (level > maxLevel) {
            return null;
        }

        MultiNode head = new MultiNode((int) (Math.random() * maxValue));

        head.children = buildChildren(level + 1, maxLevel, childrenNum, maxValue);

        return head;
    }

    public static List<MultiNode> buildChildren(int level, int maxLevel, int childrenNum, int maxValue) {
        List<MultiNode> list = null;
        int children = (int) (Math.random() * childrenNum);
        if (children > 0) {
            list = new ArrayList<>(children);
            for (int i = 0; i < children; i++) {
                MultiNode node = builder(level, maxLevel, childrenNum, maxValue);
                list.add(node);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        MultiNode head = builder(4, 5, 100);
        System.out.println(head);
    }

}
